package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"log"
	"net/http"

	"github.com/PuerkitoBio/goquery"
)

func BaiduHotSearch(url string) {
	res, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
	}

	bodyReader := bufio.NewReader(res.Body) // 包装下
	e := determineEncoding(bodyReader)
	utf8Reader := transform.NewReader(bodyReader, e.NewDecoder())
	doc, err := goquery.NewDocumentFromReader(utf8Reader)
	if err != nil {
		log.Fatal(err)
	}

	// item class
	/*doc.Find(".s-hotsearch-content .hotsearch-item").Each(func(i int, s *goquery.Selection) {
		content := s.Find(".title-content-title").Text()
		fmt.Printf("%d: %s\n", i, content)
	})*/

	// login 后
	/*doc.Find(".s-news-rank-content .news-meta-item").Each(func(i int, s *goquery.Selection) {
		content := s.Find(".title-content-title").Text()
		fmt.Printf("%d: %s.\n", i, content)
	})*/

	doc.Find("ul.list_14b > li").Each(func(i int, s *goquery.Selection) {
		url, _ := s.Find("a").Attr("href")
		content := s.Text()
		fmt.Printf("%d: %s %s\n", i, content, url)
	})
}

func determineEncoding(r *bufio.Reader) encoding.Encoding {
	bytes, e := bufio.NewReader(r).Peek(1024)
	if e != nil {
		log.Printf("Fetcher error: %v", e)
		return unicode.UTF8
	}
	e2, _, _ := charset.DetermineEncoding(bytes, "")
	return e2
}

func main() {
	// BaiduHotSearch("http://www.baidu.com")
	BaiduHotSearch("http://edu.people.com.cn")
}
